﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YRevitJig;

namespace YArchitech.HVAC
{
	public class XArrowJig : XUiJigBase
	{
		public XArrowJig.EStage JigStage { get; set; }

		public XArrowJig(UIDocument uiDoc, XYZ OriginXYZ, XYZ EndXYZ, bool IsReadProject, XYZ Direction, bool rbnStream) : base(uiDoc)
		{
			this.JigStage = XArrowJig.EStage.eHalfArrowPtOffLine;
			if (Math.Abs(OriginXYZ.X - EndXYZ.X) < 0.001)
			{
				this.IsVertical = true;
			}
			this.originXYZ = OriginXYZ;
			this.horizontalPoint = EndXYZ;
			this.verticalPoint = this.getVerticalPoint();
			this.isReadProject = IsReadProject;
			this.direction = Direction;
			this.rbnStream = rbnStream;
		}

		public XYZ PickArrowPoint(string strPrompt = "请选择标注位置:")
		{
			XYZ result = null;
			try
			{
				result = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty)
			{
				List<XYZ> list = this.setArrowLocation(base.Client2Revit(this.m_ptCur));
				if (list != null && list.Count == 4)
				{
					this.setPipeStyle(list[0], list[1], list[2], list[3], paintEventArgs);
				}
			}
		}

		private List<XYZ> setArrowLocation(XYZ ptCur)
		{
			List<XYZ> list = new List<XYZ>();
			XYZ xyz = (this.verticalPoint - this.originXYZ).Normalize();
			XYZ xyz2 = (this.horizontalPoint - this.originXYZ).Normalize();
			XYZ xyz3 = (ptCur - this.originXYZ).Normalize();
			XYZ xyz4;
			XYZ xyz5;
			XYZ xyz6;
			XYZ xyz7;
			if (this.isReadProject && this.direction.GetLength() > 0.0 && this.direction.Z != 0.0 && !this.rbnStream)
			{
				xyz2 = this.direction.Normalize();
				if (this.direction.X > 0.0)
				{
					this.IsLeft = false;
				}
				else
				{
					this.IsLeft = true;
				}
				if (this.IsVertical)
				{
					if (this.direction.Y > 0.0)
					{
						this.IsLeft = false;
					}
					else
					{
						this.IsLeft = true;
					}
				}
				if (new XYZ(xyz3.X, xyz3.Y, 0.0).AngleTo(new XYZ(xyz.X, xyz.Y, 0.0)) < Math.PI*.5)
				{
					xyz4 = this.originXYZ - this.horizontalNum / 2.0 * xyz2 + this.verticalNum * xyz;
					xyz5 = xyz4 + this.horizontalNum * xyz2;
					this.location = XArrowJig.ELocation.eUpArrow;
				}
				else
				{
					xyz4 = this.originXYZ - this.horizontalNum / 2.0 * xyz2 - this.verticalNum * xyz;
					xyz5 = xyz4 + this.horizontalNum * xyz2;
					this.location = XArrowJig.ELocation.eDownArrow;
				}
				xyz6 = xyz4 + this.horizontalNum * 0.7 * xyz2 + xyz;
				xyz7 = xyz4 + this.horizontalNum * 0.7 * xyz2 - xyz;
			}
			else if (new XYZ(xyz3.X, xyz3.Y, 0.0).AngleTo(new XYZ(xyz2.X, xyz2.Y, 0.0)) < Math.PI*.5)
			{
				if (new XYZ(xyz3.X, xyz3.Y, 0.0).AngleTo(new XYZ(xyz.X, xyz.Y, 0.0)) < Math.PI*.5)
				{
					this.IsLeft = false;
					xyz4 = this.originXYZ - this.horizontalNum / 2.0 * xyz2 + this.verticalNum * xyz;
					xyz5 = xyz4 + this.horizontalNum * xyz2;
					this.location = XArrowJig.ELocation.eUpArrow;
				}
				else
				{
					this.IsLeft = false;
					xyz4 = this.originXYZ - this.horizontalNum / 2.0 * xyz2 - this.verticalNum * xyz;
					xyz5 = xyz4 + this.horizontalNum * xyz2;
					this.location = XArrowJig.ELocation.eDownArrow;
				}
				xyz6 = xyz4 + this.horizontalNum * 0.7 * xyz2 + xyz;
				xyz7 = xyz4 + this.horizontalNum * 0.7 * xyz2 - xyz;
			}
			else
			{
				if (new XYZ(xyz3.X, xyz3.Y, 0.0).AngleTo(new XYZ(xyz.X, xyz.Y, 0.0)) < Math.PI*.5)
				{
					this.IsLeft = true;
					xyz4 = this.originXYZ + this.horizontalNum / 2.0 * xyz2 + this.verticalNum * xyz;
					xyz5 = xyz4 - this.horizontalNum * xyz2;
					this.location = XArrowJig.ELocation.eUpArrow;
				}
				else
				{
					this.IsLeft = true;
					xyz4 = this.originXYZ + this.horizontalNum / 2.0 * xyz2 - this.verticalNum * xyz;
					xyz5 = xyz4 - this.horizontalNum * xyz2;
					this.location = XArrowJig.ELocation.eDownArrow;
				}
				xyz6 = xyz4 - this.horizontalNum * 0.7 * xyz2 + xyz;
				xyz7 = xyz4 - this.horizontalNum * 0.7 * xyz2 - xyz;
			}
			if (xyz4 != null && xyz5 != null && xyz7 != null && xyz6 != null)
			{
				list.Add(xyz4);
				list.Add(xyz5);
				list.Add(xyz6);
				list.Add(xyz7);
			}
			return list;
		}

		private void setPipeStyle(XYZ startPt, XYZ endPt, XYZ upTrianglePt, XYZ downTrianglePt, PaintEventArgs paintEventArgs)
		{
			XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
			if (this.JigStage == XArrowJig.EStage.eAllArrowPtOffLine)
			{
				this.MoveVec = (startPt + endPt) / 2.0 - this.originXYZ;
				new XLine2D(base.Revit2Client(startPt), base.Revit2Client(endPt)).Draw(g);
				new XLine2D(base.Revit2Client(upTrianglePt), base.Revit2Client(endPt)).Draw(g);
				new XLine2D(base.Revit2Client(downTrianglePt), base.Revit2Client(endPt)).Draw(g);
				return;
			}
			if (this.JigStage == XArrowJig.EStage.eAllArrowPtOnLine)
			{
				XYZ xyz = (startPt + endPt) / 2.0 - this.originXYZ;
				new XLine2D(base.Revit2Client(startPt - xyz), base.Revit2Client(endPt - xyz)).Draw(g);
				new XLine2D(base.Revit2Client(upTrianglePt - xyz), base.Revit2Client(endPt - xyz)).Draw(g);
				new XLine2D(base.Revit2Client(downTrianglePt - xyz), base.Revit2Client(endPt - xyz)).Draw(g);
				return;
			}
			if (this.JigStage != XArrowJig.EStage.eHalfArrowPtOffLine)
			{
				if (this.JigStage == XArrowJig.EStage.eHaltArrowPtOnLine)
				{
					XYZ xyz2 = (startPt + endPt) / 2.0 - this.originXYZ;
					new XLine2D(base.Revit2Client(startPt - xyz2), base.Revit2Client(endPt - xyz2)).Draw(g);
					if (this.location == XArrowJig.ELocation.eUpArrow)
					{
						new XLine2D(base.Revit2Client(upTrianglePt - xyz2), base.Revit2Client(endPt - xyz2)).Draw(g);
						return;
					}
					new XLine2D(base.Revit2Client(downTrianglePt - xyz2), base.Revit2Client(endPt - xyz2)).Draw(g);
				}
				return;
			}
			this.MoveVec = (startPt + endPt) / 2.0 - this.originXYZ;
			new XLine2D(base.Revit2Client(startPt), base.Revit2Client(endPt)).Draw(g);
			if (this.location == XArrowJig.ELocation.eUpArrow)
			{
				new XLine2D(base.Revit2Client(upTrianglePt), base.Revit2Client(endPt)).Draw(g);
				return;
			}
			new XLine2D(base.Revit2Client(downTrianglePt), base.Revit2Client(endPt)).Draw(g);
		}

		private XYZ getVerticalPoint()
		{
			double z = this.originXYZ.Z;
			double num = this.originXYZ.X - this.horizontalPoint.X;
			double num2 = this.originXYZ.Y - this.horizontalPoint.Y;
			if (Math.Abs(num2) < 0.0001)
			{
				return new XYZ(0.0, 10.0, z) + this.originXYZ;
			}
			if (Math.Abs(num) < 0.0001)
			{
				return new XYZ(10.0, 0.0, z) + this.originXYZ;
			}
			double num3 = 10.0;
			return new XYZ(-(num3 * num2) / num, num3, 0.0) + this.originXYZ;
		}

		private XArrowJig.ELocation location;

		private XYZ originXYZ;

		private XYZ horizontalPoint;

		private XYZ verticalPoint;

		public double verticalNum = 1.0;

		private double horizontalNum = 6.0;

		public XYZ MoveVec = new XYZ();

		public bool IsLeft = true;

		public bool IsVertical;

		public bool isReadProject;

		public XYZ direction = new XYZ();

		private bool rbnStream = true;

		public enum EStage
		{
			eAllArrowPtOffLine,
			eHalfArrowPtOffLine,
			eAllArrowPtOnLine,
			eHaltArrowPtOnLine
		}

		public enum ELocation
		{
			eUpArrow,
			eDownArrow
		}
	}
}
